package org.gap.jseed.contract;

import org.gap.jseed.contract.annotation.Condition;

public abstract class ValidationWriter implements Validation {
	
	protected String createTestCondition(String src) {
		return String.format("if (!(%s)) ", src);
	}
	
	protected String createPreConditionViolationException(Condition condition) {
		if (condition.label().isEmpty()) {
			return createException(withMessage("The precondition [%s] was not met", condition.value()));
		} else {
			return createException(condition.label());
		}
	}

	protected String createPostConditionViolationException(Condition condition) {
		if (condition.label().isEmpty()) {
			return createException(withMessage("The postcondition [%s] was not met", condition.value()));
		} else {
			return createException(condition.label());
		}
	}

	public String createSpecialConditionViolationException(String withMessage) {
		return createException(withMessage);
	}
	
	private String withMessage(String message, String src) {
		return String.format(message, src);
	}
	
	public String createException(String withMessage) {
		return String.format("{  throw new org.gap.jseed.contract.ContractViolation(\"%s\");  }\n", fixMessage(withMessage));
	}

	private Object fixMessage(String withMessage) {
		return withMessage.replace("$1", "\" + $1 + \"");
	}
}
